//
//  KIcon.swift
//  OnemeetingDemo
//
//  Created by 张衡 on 2020/5/22.
//  Copyright © 2020 www.onemeeting.1meetingIos. All rights reserved.
//

import Foundation

struct KIconGroup {
    var image:String!
    var title:String!
    init(title:String,image:String) {
        self.image = image
        self.title = title
    }
}

class KIcon: MSView {
    var data:KIconGroup?
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    convenience init(data:KIconGroup) {
        self.init()
        self.data = data
        self.setupUI()
        self.loadData()
    }
    
    func loadData() {
        self.stateBlock = { state in
            self.selectView.isHidden = true
            switch state {
            case .normal:
                self.layer?.backgroundColor = NSColor.clear.cgColor
            case .highlight:
                self.layer?.backgroundColor = color_gray(220).withAlphaComponent(0.1).cgColor
            case .hover:
                self.layer?.backgroundColor = color_gray(220).withAlphaComponent(0.1).cgColor
            case .selected:
                self.selectView.isHidden = false
                self.layer?.backgroundColor = color_gray(220).withAlphaComponent(0.4).cgColor
            }
        }
        
        if let data = self.data {
            self.imageV.image = NSImage.imageNamed(data.image)
            self.title.stringValue  = data.title
        }
        
    }
    
    
    func setupUI() {
        self.wantsLayer = true
        
        self.addSubview(imageV)
        imageV.snp.makeConstraints { (make) in
            make.size.equalTo(30)
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(5)
        }
        
        self.addSubview(title)
        title.snp.makeConstraints { (make) in
            make.width.equalToSuperview()
            make.height.equalTo(20)
            make.bottom.equalToSuperview()
            make.centerX.equalToSuperview()
        }
        
        self.addSubview(selectView)
        selectView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    var imageSize:CGSize = CGSize.init(width: 30, height: 30) {
        didSet {
            self.imageV.snp.updateConstraints { (make) in
                make.size.equalTo(imageSize)
            }
        }
    }
    
    
    lazy var selectView: NSView = {
        let view = NSView()
        view.wantsLayer = true
        view.layer?.backgroundColor = color_gray(40).withAlphaComponent(0.5).cgColor
        view.isHidden = true
        return view
    }()
    lazy var imageV: NSImageView = {
        let imageV = NSImageView.init(image: NSImage.imageNamed("icon_custom")!)
        return imageV
    }()
    
    lazy var title: NSTextField = {
        let lab = NSTextField.label(title: "问卷调查", textColor: color_gray(255), font: NSFont.systemFont(ofSize: 12))
        lab.alignment = .center
        lab.sizeToFit()
        return lab
    }()
}
